package com.example.myapplication;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Telephony;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class SmsReceiver extends BroadcastReceiver {

    private Context mContext;
    public static final String SMS_RECEIVED_ACTION = "android.provider.Telephony.SMS_RECEIVED";
    public static final String SMS_DELIVER_ACTION = "android.provider.Telephony.SMS_DELIVER";

    @Override
    public void onReceive(Context context, Intent intent) {

        this.mContext = context;

        Toast.makeText(context, "接收短信执行了.....", Toast.LENGTH_LONG).show();
        Log.e("日志：广播：：onReceive", isOrderedBroadcast() + "");
        Log.e("日志：onReceive...", "-接收短信执行了......"+intent.getStringExtra("sele"));
        String action = intent.getAction();
        if (SMS_RECEIVED_ACTION.equals(action) || SMS_DELIVER_ACTION.equals(action) ) {
            Log.e("日志：onReceive。。。", "开始接收短信.....");
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                if (pdus != null && pdus.length > 0) {
                    SmsMessage[] messages = new SmsMessage[pdus.length];
                    for (int i = 0; i < pdus.length; i++) {
                        byte[] pdu = (byte[]) pdus[i];
                        messages[i] = SmsMessage.createFromPdu(pdu);
                    }
                    for (SmsMessage message : messages) {
                        String content = message.getMessageBody();// 得到短信内容
                        String sender = message.getOriginatingAddress();// 得到发信息的号码
                        Date date = new Date(message.getTimestampMillis());
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        format.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));
                        String dateContent = format.format(date);

                        if ((sender.equals("121234101") || content.contains("河南省交警"))){
                            Log.e("日志：短信时间：：： ", format.format(date) + " ");
                            Log.e("日志：发信息的号码 ", sender + " ");
                            Log.e("日志：得到短信内容 ", content + " ");
                            vibrate(context);
                            //处理接收的短信
                            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                            // 创建通知
                            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, ".SmsReceiver")
                                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                                    .setContentTitle("赶紧挪车了")
                                    .setContentText("短信内容：" + intent.getStringExtra("sms_body"))
                                    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
                            // 显示通知
                            notificationManager.notify(1, builder.build());

                            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

                            this.abortBroadcast();// 中止
                        }
                    }
                }
            }
        }

//        Log.d("SmsReceiver", "onReceive.....");
//
//        if (intent.getAction() != null && intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
//            // 获取短信数据
//            SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
//            if (messages != null && messages.length > 0) {
//                StringBuilder smsBody = new StringBuilder();
//                for (SmsMessage message : messages) {
//                    smsBody.append(message.getMessageBody());
//                }
//
//                // 检查特定内容
//                String specificContent = "流量查询";
//                if (smsBody.toString().contains(specificContent)) {
//                    // 如果收到包含特定内容的短信，执行震动提醒
//                    vibrate(context);
//
//                    //处理接收的短信
//                    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//
//                    // 创建通知
//                    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "channel_id")
//                            .setSmallIcon(R.drawable.ic_launcher_background)
//                            .setContentTitle("新短信")
//                            .setContentText("短信内容：" + intent.getStringExtra("sms_body"))
//                            .setPriority(NotificationCompat.PRIORITY_DEFAULT);
//
//                    // 显示通知
//                    notificationManager.notify(1, builder.build());
//                }
//            }
//        }
    }

    private void vibrate(Context context) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        if (vibrator != null) {
            // 震动5秒钟
            vibrator.vibrate(5000);
        }
    }

}
